package klimuts.periodicals.admin.actions;

import java.util.LinkedList;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import klimuts.periodicals.admin.Order;
import klimuts.periodicals.data.DataSource;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Action retrieve id and new status values from URL and update order status
 * in database. If updating is success then action update same order in session
 * because at next time orders will be getting from session.   
 * @author Siarhei Klimuts
 *
 */
public class UpdateOrder extends ActionSupport implements SessionAware{
	private long id;
	private byte status;
	private Map<String, Object> session;
	
	public String execute() throws Exception{
		if (DataSource.orderUpdate(id, status)) {
			LinkedList<Order> orders=(LinkedList<Order>)session.get("orders");
			if (orders!=null)
				for (Order order: orders)
					if (order.getId()==id) {
						order.setStatus(status);
						break;
					}
		} else
			addActionError(getText("order.update.error"));
		return "input";
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public byte getStatus() {
		return status;
	}

	public void setStatus(byte status) {
		this.status = status;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session=session;
	}

}
